//
//  UseCarTypesVC.m
//  NtoccShipper
//
//  Created by mac on 2019/12/3.
//  Copyright © 2019 zhuchongjin. All rights reserved.
//

#import "UseCarTypesVC.h"
#import "UseCarTypeModel.h"
#import "UseCarTypeCell.h"

@interface UseCarTypesVC ()<UITableViewDelegate,UITableViewDataSource>{
//    UIView* _imgParent;
//    int indexHedaer;  // 记录图片的位置
//    int indexSelect;
}
@property (nonatomic,strong) CJTableView *tableView;
@property (nonatomic,strong) UseCarTypeCell *useCarTypeCell;

//@property (nonatomic,strong) NSMutableArray *arrHeaderTitle;
@property (nonatomic,strong) NSMutableArray *arrCellModel;
@property (weak, nonatomic) IBOutlet UIButton *btnSave;
@property (nonatomic,strong) CJLabel *labTopTitle;
@end

@implementation UseCarTypesVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitleName:CJLString(@"用车类型")];
    [self setBackLeftButton:@""];
//    [self setSecondAppNavigationBarStyle];
    self.btnSave.backgroundColor = [UIColor cjMainColor];
    [self initData];
    self.useCarTypeCell = [[UseCarTypeCell alloc]initWithFlex:nil reuseIdentifier:nil];
    
    [self.view addSubview:self.labTopTitle];
    [self.view addSubview:self.tableView];

}

- (CJLabel *)labTopTitle{
    if (!_labTopTitle) {
        _labTopTitle = CJLabel.labInit
        .labFrame(CGRectMake(0, kTopHeight, kScreenWidth, 30))
        .labTextColor([UIColor cjMainColorText])
        .labFontSize(14)
        .labBgColor([UIColor cjWhiteColor])
        .labText(@"     *点击选中")
        ;
    }
    return _labTopTitle;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
//    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
   
    
      if ([self.dictData isKindOfClass:[NSNull class]] || [self.dictData isEqual:[NSNull null]] || self.dictData.count == 0) {
            [self loadData];
      }else{
          [self.arrCellModel removeAllObjects];
          self.arrCellModel = (NSMutableArray *)self.dictData[@"cellInfo"];
          [self.tableView reloadData];
      }
      
}

- (void)initData{
    self.arrCellModel = [[NSMutableArray alloc]init];
}
- (void)loadData{
    
    WS(ws);
    [CJNetWork getSenGoodsDicsTypeinfo:CJGetSendgoodsvehicleLengthClass Success:^(id  _Nullable responseObject) {
        DLog(@"+++++ %@",responseObject);
        
        [ws.arrCellModel removeAllObjects];
        NSArray *list = (NSArray *)responseObject;
        if (list.count > 0) {
            
            for (int i = 0; i < list.count; i++) {
                
                NSMutableArray *arr = [[NSMutableArray alloc]init];
                NSDictionary *dic = list[i];
                NSArray *listCell = dic[@"dictList"];
                NSMutableArray *arrListCell = [[NSMutableArray alloc]initWithArray:listCell];
                [arrListCell reverse];

                [arrListCell enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                   NSError *error;
                   UseCarTypeModel *model = [[UseCarTypeModel alloc]initWithDictionary:obj error:&error];
                    model.selected = NO;
                    [arr addObject:model];
                }];
                [ws.arrCellModel addObject:arr];
            }
        }
        
//        DLog(@"+++++++ %@",ws.arrCellModel);
        [ws.tableView reloadData];
    }];
    
    
}




- (IBAction)btnSaveAction:(id)sender {
    
        NSString *strSelect = @"";
        if (self.arrCellModel.count > 0) {
            
            for (int i = 0; i < self.arrCellModel.count; i++) {
                
                NSMutableArray *array = self.arrCellModel[i];
                
                if (array.count > 0) {
                    
                    for (int y = 0; y < array.count; y++) {
                        UseCarTypeModel *model = array[y];
                        
                        if (model.selected) {
                            strSelect = CJStringWithFormat(@"%@,%@",strSelect,model.name);
                        }
                    }
                }
                
            }
            
            if ([strSelect isEqualToString:@""]) {
//                [CJAlert showStringDismissWithDelay:@"请选择分类"];
                if (self.blockCancleInfo) {
                    self.blockCancleInfo(@"");
                }
                
            } else {
                NSDictionary *dic = @{
                    @"cellInfo":self.arrCellModel,
                    @"index":[strSelect substringFromIndex:1],
                };
                
                if (self.blockSelectInfo) {
                    self.blockSelectInfo(dic);
                }
                
        //        [CJAlert showStringDismissWithDelay:strSelect];
              
            }
            [self pop];
        }else{
            [CJAlert showStringDismissWithDelay:@"请选择分类"];

        }
    
}



#pragma mark----tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    return self.arrNewsList.count;
    return self.arrCellModel.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    
    static NSString *identifier = @"UseCarTypeCell";
     UseCarTypeCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
     if (cell == nil) {
         cell = [[UseCarTypeCell alloc] initWithFlex:nil reuseIdentifier:identifier];
         cell.selectionStyle = UITableViewCellSelectionStyleNone;
//         [cell setCellInfoWithDic:nil];

     }
//    SelectGoodsNameHeaderModel *model = self.arrHeaderTitle[indexHedaer];
//    [cell setCellInfoWithMutaArray:self.arrCellModel[indexPath.row] withHeaderModel:model];
//    [cell setCellInfoWithMutaArray:self.arrCellModel[indexPath.row] ];
    [cell setCellInfoWithMutaArray:self.arrCellModel[indexPath.row] index:indexPath.row];
     return cell;
}

- (CJTableView *)tableView{
    if (!_tableView) {
        _tableView = CJTableView.tbInitFrameStyle(CGRectMake(0, self.labTopTitle.bottom + CJTableToTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT-kTopHeight - self.btnSave.height - CJTableToTopHeight - self.labTopTitle.height),UITableViewStylePlain)
        .tbDelegate(self)
        .tbDataSource(self)
        .tbSetOther(YES)
        .tbEstimatedRowHeight(YES)
//        .tbMjHeadFooter(YES)
        ;
//        WS(ws);
//        _tableView.blockMjHeader = ^(BOOL isheader) {
//
//            if (isheader) {
//                [ws downRefresh];
//            } else {
//                [ws upRefresh];
//            }
//
//        };

    }
    
    return _tableView;
}
@end



